<?php

if (!isset($_REQUEST['entry']) || !is_numeric($_REQUEST['entry'])) {
  error_obj::setError(new error_obj("bad request entry", __FILE__, 'line 4 -> redirect'), UNEXCEPTED);
  base_obj::redirect(ROOT);
}

$entryId = (int) $_REQUEST['entry'];

$user_obj = new user_obj();
$cash_obj = new cash_obj();
$lock_obj = new lock_obj();

if (isset($_REQUEST['action'])) {
  switch ($_REQUEST['action']) {
    case 'edit_entry':
      $in_data = array();
      $other_in_data = array();
      $in_data['user'] = $_REQUEST['entry_data']['user'];
      $in_data['date'] = base_obj::convertDateTo($_REQUEST['entry_data']['date']);
      $in_data['type_of_move'] = $_REQUEST['entry_data']['moveType'];
      $in_data['description'] = $_REQUEST['entry_data']['description'];
      if (isset($_REQUEST['entry_data']['s']))
        $in_data['s'] = $_REQUEST['entry_data']['s'];
      else
        $in_data['s'] = 0;
      if (in_array($_REQUEST['entry_data']['moveType'], $GLOBALS['positiveMovesOfCash'])) {
        $in_data['amount'] = $_REQUEST['entry_data']['amount'];
        if (isset($_REQUEST['other_entry']) && ($_REQUEST['other_entry'] != -1)) {
          $cash_obj->deleteEntryById($_REQUEST['other_entry'], true);
        }
        $in_data['transfer_between'] = -1;
      } elseif ($_REQUEST['entry_data']['moveType'] == $GLOBALS['bothMovesOfCash']) {
        $in_data['amount'] = -$_REQUEST['entry_data']['amount'];
        $in_data['transfer_between'] = time();
        $other_in_data = $in_data;
        $other_in_data['amount'] = $_REQUEST['entry_data']['amount'];
        $other_in_data['user'] = $_REQUEST['entry_data']['recipient'];
      } else {
        $in_data['amount'] = -$_REQUEST['entry_data']['amount'];
        if (isset($_REQUEST['other_entry']) && ($_REQUEST['other_entry'] != -1)) {
          $cash_obj->deleteEntryById($_REQUEST['other_entry'], true);
        }
        $in_data['transfer_between'] = -1;
      }

      $result = $cash_obj->editEntry($in_data, $entryId);
      if (error_obj::isError($result)) {
        error_obj::setError($result, UNEXCEPTED);
      } else {
        if (!empty($other_in_data)) {
          if (isset($_REQUEST['other_entry']) && ($_REQUEST['other_entry'] != -1)) {
            $res = $cash_obj->editEntry($other_in_data, $_REQUEST['other_entry']);
          } else {
            $other_in_data['created'] = date("Y-m-d H:i:s");
            $other_in_data['who_added'] = $_SESSION['user']['user_id'];
            $res = $cash_obj->addEntry($other_in_data);
          }
          if (error_obj::isError($res)) {
            error_obj::setError($res, UNEXCEPTED);
          }
        }
        error_obj::setGood('entry was updated!');
        base_obj::redirect(ROOT . '?input=state_of_cash');
      }

      break;
  }
}

$entry_data = $cash_obj->getEntry($entryId);

if (error_obj::isError($entry_data)) {
  error_obj::setError($entry_data, UNEXCEPTED);
} elseif (empty($entry_data)) {
  error_obj::setError(new error_obj("empty entry", __FILE__, 'get entry data'), EMPTY_entry);
} else {

  if ($lock_obj->isEntryLocked(base_obj::convertDateTo($entry_data['date'], 'dd/mm/yyyy', 'date')))
    base_obj::redirect(ROOT . '?input=state_of_cash&locked_period=1');

  if (($entry_data['type_of_move'] == $GLOBALS['bothMovesOfCash']) && ($entry_data['amount'] >= 0)) {
    $other_entry = $cash_obj->getEntryByTransfer_between($entry_data['transfer_between']);
    if (!error_obj::isError($other_entry))
      base_obj::redirect(ROOT . '?input=edit_entry&entry=' . $other_entry['Id']);
  }

  $filter = array();
  $filter['active'] = 1;
  $filter['client'] = 11;
  $entry['recipients'] = $user_obj->getUsers('name, surname, users_id', $filter);
  $entry['cash_types'] = $cash_obj->getCashMoveType();
  $entry['bothMovesOfCash'] = $GLOBALS['bothMovesOfCash'];

  $GLOBALS['template_data'] = $entry;
  $GLOBALS['template_data']['entry_data'] = $entry_data;
  if ($entry_data['amount'] < 0)
    $GLOBALS['template_data']['entry_data']['amount'] = - $entry_data['amount'];
  if ($entry_data['amount'] != -1)
    $GLOBALS['template_data']['entry_data']['other'] = $cash_obj->getEntryByTransfer_between($entry_data['transfer_between'], '>');
  $GLOBALS['template_data']['entry_data']['date_formated'] = base_obj::convertDateTo($GLOBALS['template_data']['entry_data']['date'], 'dd/mm/yyyy', 'date');
}
?>
